﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ModelXML;
using Etapa1.Import;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using PloobsEngine;
using Etapa1.ModelDescription;

namespace Etapa1.SofRender.Sample
{
    public class SModelo
    {
        VertexType[] vertexType;

        public VertexType[] VertexType
        {
            get { return vertexType; }            
        }
        int[] indices;

        public int[] Indices
        {
            get { return indices; }            
        }
        SEffect effect;

        public SEffect Effect
        {
            get { return effect; }
            set { effect = value; }
        }
        ModelXML.Modelo modelo;

        public SModelo(Model model, string texName)
        {
            this.difuse = EngineStuff.CustomContentManager.GetAsset<Texture2D>(texName);

            if (model.Meshes.Count > 0)
                throw new Exception("just ONE");

            if (model.Meshes[0].MeshParts.Count > 0)
                throw new Exception("just ONE MAN !!!! ");

            ExtractModelInformations ei = new ExtractModelInformations();
            List<VertexPositionNormalTexture> verts = new List<VertexPositionNormalTexture>();
            List<int> indices = new List<int>();
            ei.ExtractInfos(model.Meshes[0].IndexBuffer ,model.Meshes[0].VertexBuffer,model.Meshes[0].MeshParts[0].PrimitiveCount,model.Meshes[0].MeshParts[0].NumVertices,model.Meshes[0].MeshParts[0].VertexStride, out verts, out indices);

            VertexType[] vertexType = new VertexType[modelo.Vertices.Count()];
            int i = 0;                
            foreach (var item in verts)
            {                
                    VertexType vt = new VertexType();
                    vt[Slots.POSITION0] = ShaderOp.toVector4(item.Position);
                    vt[Slots.TEXTCOORD0] = ShaderOp.toVector4(item.TextureCoordinate);
                    vt[Slots.NORMAL0] = ShaderOp.toVector4Norm(item.Normal);
                    vertexType[i] = vt;
                    i++;                
            }
            this.indices = indices.ToArray();
            this.vertexType = vertexType;
            world = Matrix.Identity;
        }

        public SModelo(ModelXML.Modelo modelo)
        {
            this.modelo = modelo;
            effect = new BasicShader();
            Init();
        }

        Matrix world;

        public Matrix World
        {
            get { return world; }            
        }

        Texture2D difuse;

        public Texture2D Difuse
        {
            get { return difuse; }
            set { difuse = value; }
        }


        void Init()
        {
            indices = modelo.Indices;
            vertexType = new VertexType[modelo.Vertices.Count()];
            int i = 0;
            foreach (var item in modelo.Vertices)
            {
                VertexType vt = new VertexType();
                vt[Slots.POSITION0] = ImporterHelper.PosToVector4(item.Position);
                vt[Slots.TEXTCOORD0] = ImporterHelper.toVector4(item.TextureCoord);
                vt[Slots.NORMAL0] = ImporterHelper.NormToVector4(item.Normal);
                vertexType[i] = vt;
                i++;
            }
            world = ImporterHelper.CreateTransformation(modelo.Transformation);
            
            foreach (var item in modelo.Maps)
            {
                String path = item.Path;
                if (path.Contains("."))
                {
                    path = path.Split('.')[0];
                }
                if(item.MapType == MapTypes.Diffuse)
                {
                    difuse = EngineStuff.CustomContentManager.GetAsset<Texture2D>("Textures//" + path);
                }
            }           
        }
    }
}
